<?php
    require_once MODULE.'/mod_admin/models/MenuModel.php';
    
    $task = (isset($_REQUEST["task"]) && $_REQUEST["task"]!=""  )?$_REQUEST["task"]:"list";
    
    $temp['title'] = 'Quản trị menus';
    $temp['head_title'] = 'Quản lý menus';
    $temp['mess'] = '';    
    
    $menuModel = new MenuModel($this->pdo);
    
    $templateView = '';
    $firstArray = $allMenu = array();
    $menu = '';
    switch ($task) {
    case "list":
        $allMenu = $menuModel->getAllMenu();
        $menu   = $menuModel->getHTML($allMenu);
        $templateView = 'menu_list.php';
        break;
    case "add":
        if (!empty($_POST))
        {
            $menuModel->insertMenu($_POST);
        }
        $url = DOMAIN.'/'."application.php?mod=mod_admin&control=Menus";
        header('Location: '.$url);        
        break;
    case "edit":
        if (!empty($_POST))
        {
            if($menuModel->editMenu($_POST))
            {
                $temp['mess'] = "Chỉnh sửa menu thành công!";
            }
            else{
                $temp['mess'] = "Chỉnh sửa menu không thành công!";
            }
        }    
        $idMenu = $_REQUEST['id'];
        $firstArray = $menuModel->getMenuByID($idMenu);
        $templateView = 'menu_edit.php';
        break;
    case "del":
       //
       $id = $_REQUEST['delete_id'];
       // reset all menu child item parent id with 0
       $menuModel->updateChildItem($id);
       // delete this item
       $menuModel->deleteItemMenu($id);
       
       $url = DOMAIN.'/'."application.php?mod=mod_admin&control=Menus";
       header('Location: '.$url);       
    break;
    case "reorder":
        $menuModel->reorderMenu($_POST);       
    break;        
    default:
        $templateView = 'menu_list.php';
    }
    require_once MODULE.'/mod_admin/views/layout.php';    
?>